<?php

namespace app\api\controller;

use app\common\model\AritcleModel;
use think\cache\driver\Redis;
use think\Controller;
use think\Request;

class Article extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        //页码
        $page = $request->param('page');
        //显示条数
        $limit = $request->param('limit');
        //搜索
        $key = $request->param('key');

//        dump($this->aid);
        $db = new AritcleModel();

        //判断搜索是否纯在搜索 否则就用缓存
        if (isset($key)&&!empty($key)){
            $db = $db->where('title','like','%'.$key.'%');
            $info = $db->page($page)->limit($limit)->order('sort desc')->select();
            $total = $db->count('id');
            return json(['code'=>1,'data'=>$info,'total'=>$total]);
        }else{
            //通过redis读取内容
            $redis = new Redis();
            $redis->select('1');
            //统计原来的是lSize，php7以上更新为lLen
            $count = $redis->lLen("artcle_id");//获取链表的长度
            //页大小
            $page_size =(!empty($limit))?$limit:10;
            //当前页码
            $page_num=(!empty($page))?$page:1;
            //页总数
            $page_count = ceil($count/$page_size);
            //分页出来的id
            $ids = $redis->lrange("artcle_id",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1));
            //通过文章id查询出来
            foreach($ids as $v){
                $data[]=$redis->hgetall("artcle:".$v);
            }
            return json(['code'=>2,'data'=>$data,'total'=>$count]);
        }
    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $db = new AritcleModel();
//        判断是否有id有就修改没有就添加
        if(isset($data['id'])&&!empty($data['id'])){
            $res = $db->update($data,['id'=>$data['id']]);
        }else{
            $res = $db->create($data);
        }

        //更新添加成功后到缓存中
        if($res==true){
            $sres = $db->where('id',$res['id'])->find()->toArray();
            //先更新缓存在更新数据库
            $redis = new Redis();
            //存入1的库
            $redis->select('1');
            //插入redis缓存中
            $redis->hMset('artcle:'.$res['id'],$sres);
            return json(['code'=>1,'msg'=>'成功']);
        }else{
            return json(['code'=>0,'msg'=>'失败']);
        }
    }



    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $db = new AritcleModel();
        $res = $db->where('id',$id)->delete();
        //删除成功后同时删除缓存
        if ($res){
            //先更新缓存在更新数据库
            $redis = new Redis();
            //存入1的库
            $redis->select('1');
            //查询是否有
            $id_name_artcle = $redis->hget("artcle:".$id,"id");
            $redis->del("artcleid:id".$id.$id);
            $a=$redis->lrem("artcle_id",$id);

            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }

    }
}
